package com.sunday.android.fangdaifu.bukong.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.sunday.common.R;
import com.sunday.common.utils.PixUtils;
import com.sunday.common.utils.ToastUtils;


/**
 * 仿IOS的弹出框
 *
 * @来自网上
 */
public class EditAlertView extends Dialog {

    private Context context;
    private String title;
    private String message;
    private String buttonLeftText;
    private String buttonRightText;
    private ClickListenerInterface clickListenerInterface;
    private int type;

    public EditAlertView(Context context, String title, String message,
                         String buttonLeftText, String buttonRightText) {
        super(context, R.style.UIAlertViewStyle);

        this.context = context;
        this.title = title;
        this.message = message;
        this.buttonLeftText = buttonLeftText;
        this.buttonRightText = buttonRightText;
    }

    public EditAlertView(Context context, String title, String message,
                         String buttonLeftText, String buttonRightText, int type) {
        super(context, R.style.UIAlertViewStyle);

        this.context = context;
        this.title = title;
        this.message = message;
        this.buttonLeftText = buttonLeftText;
        this.buttonRightText = buttonRightText;
        this.type = type;
    }

    public interface ClickListenerInterface {
        void doLeft();

        void doRight(String temp);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        inite();
    }

    EditText editText2;

    public void inite() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.ui_edit_view, null);
        setContentView(view);

        editText2 = (EditText) view.findViewById(R.id.tvMessage);
        editText2.setTextSize(14);
        TextView tvLeft = (TextView) view.findViewById(R.id.tvBtnLeft);
        TextView tvRight = (TextView) view.findViewById(R.id.tvBtnRight);
        TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);

        if ("".equals(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setText(title);
        }
        editText2.setText(message);
        if (type == 1) {
            editText2.setHint("输入倾斜角度.东:正数，西:负数");
            editText2.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_SIGNED|InputType.TYPE_NUMBER_FLAG_DECIMAL);
        } else if (type == 2) {
            editText2.setHint("输入倾斜角度。南:正数，北:负数");
            editText2.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_SIGNED|InputType.TYPE_NUMBER_FLAG_DECIMAL);
        }
        editText2.addTextChangedListener(textWatcher);
        tvLeft.setText(buttonLeftText);
        tvRight.setText(buttonRightText);

        tvLeft.setOnClickListener(new clickListener());
        tvRight.setOnClickListener(new clickListener());

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics();

        lp.width = (int) (d.widthPixels * 0.8);
        dialogWindow.setAttributes(lp);
    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }

    private class clickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {

            String tag = (String) v.getTag();
            switch (tag) {
                case "tvBtnLeft":
                    clickListenerInterface.doLeft();
                    break;
                case "tvBtnRight":
                    String temp = editText2.getText().toString();
                    if (TextUtils.isEmpty(temp)) {
                        ToastUtils.showToast(context, "请输入初始值");
                        return;
                    }
                    clickListenerInterface.doRight(temp);
                    break;
                default:
                    break;
            }
        }
    }

    private TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            if (TextUtils.isEmpty(s)) {
                return;
            }
            if (s.toString().startsWith(".")) {
                s = "0.";
                editText2.setText(s);
                editText2.setSelection(s.length());
            }
            if (s.toString().contains(".")) {
                if (s.toString().indexOf(".") != s.toString().lastIndexOf(".")) {
                    s = s.toString().substring(0, 1) + ".";
                    editText2.setText(s);
                    editText2.setSelection(s.length());
                }
                if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                    s = s.toString().subSequence(0, s.toString().indexOf(".") + 3);
                    editText2.setText(s);
                    editText2.setSelection(s.length());
                }
            }
            if (s.toString().trim().substring(0).equals(".")) {
                s = "0" + s;
                editText2.setText(s);
                editText2.setSelection(2);
            }

            if (s.toString().startsWith("0")
                    && s.toString().trim().length() > 1) {
                if (!s.toString().substring(1, 2).equals(".")) {
                    editText2.setText(s.subSequence(0, 1));
                    editText2.setSelection(1);
                }
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }

    };

}
